"use strict";
const CONFIG = require('../../../../../common/config');
const moment = require('moment');
const async = require('async');
const luaUtil = require('../commonUtils/luaUtil');
const superagent = require('superagent');
const retUtil = require('../commonUtils/retUtil');
const docConvertUtil = require('../commonUtils/docConvertUtil');
const superAgentUtil = require('../commonUtils/superAgentUtil');
const CONSTANT =  require("../../const/CONST");

const designsVideosUtil = (function () {

    function designsVideosUtil() {
    }


    /**
     * 获取视频模板列表
     * @author mk
     * @date   2018-5-30
     *
     * @param token      必须   使用的token
     * @param pageNum    必须  页码
     * @param pageSize   必须  页容量
     * @param callback   参见：https://doc.yijianxiu.com/doc/open_api/api_design_video_list.html
     */
    designsVideosUtil.getDesignList= function (token,pageNum,pageSize,callback) {
        luaUtil.getSetIds(token,function (err,setIds) {
            if (err) {
                console.log("routes/v1/utils/routerUtils/designsVideosUtil.js getDesignList getSetIds err " + err);
                return callback(err, null);
            }
            //测试库地址路径
            let url = CONFIG.API_SERVER_FULL_URL + "/yjx/v1/document/getTmplSetContent_v1_0";
            //本地路径
            //let url =  "127.0.0.1:3000/yjx/v1/document/getTmplSetContent_v1_0";
            let param = {
                showTypes: null,
                keyword: null,
                setIds: setIds,
                orderBy: 4,
                pageNum: pageNum,
                pageSize: pageSize,
                withTotalPage: 0,
                durations: JSON.stringify([1, 2, 3])
            };
            superAgentUtil.reqRes(url, param, function (err, result) {
                if (err || !result) {
                    return callback(CONSTANT.CODE.UNKNOWN_ERR, result||[]);
                }
                let data = {};
                data.list = [];
                if (!result.data.list || result.data.list.length <= 0) {
                    return callback(null, {list: list});
                }
                for (let i = 0; i < result.data.list.length; i++) {
                    let oneRes = result.data.list[i];
                    let searchDate = {
                        design_id: oneRes.id,
                        name: oneRes.name,
                        thumb: oneRes.thumb,
                        video: oneRes.video,
                        use_counts: oneRes.useCounts,
                        w: oneRes.w,
                        h: oneRes.h,
                        author: oneRes.author,
                        duration: oneRes.duration
                    };
                    data.list.push(searchDate);
                }
                return callback(null, data);
            });
        });
    };


    /**
     * 获取视频模板信息
     * @author mk
     * @date  2018-5-30
     *
     * @param token       必须  查询的token
     * @param designId    必须  设计模板id
     * @param callback   参见：https://doc.yijianxiu.com/doc/open_api/api_design_video_content.html
     */
    designsVideosUtil.getDesignContent= function (token,designId,callback) {

        const totalTasks = [];
        // 1. 根据 lua 获取用户 id
        totalTasks.push(function (callback) {
            luaUtil.getUserId(token,function (err,userId) {
                if (err) {
                    console.log("routes/v1/utils/routerUtils/designsVideosUtil.js getDesignContent  err " + err);
                    return callback(CONSTANT.CODE.PERMISSION_DENIED,null);
                }
                const data = {};
                data.userId = userId;
                return callback(err, data);
            });
        });

        // 2. 向 API 请求数据
        totalTasks.push(function (data, callback) {
            //测试库地址路径
            let url =  CONFIG.API_SERVER_FULL_URL +"/yjx/v1/document/getDocContent_v1_0";
            //本地路径
            //let url =  "127.0.0.1:3000/yjx/v1/document/getDocContent_v1_0";
            let param = {
                userId : data.userId,
                id : designId,
                isTmpl : 1
            };
            superAgentUtil.reqRes(url, param, function (err, res){
                if (err) {
                    return callback(CONSTANT.CODE.UNKNOWN_ERR, null);
                }
                data.apiResult = res;
                return callback(null, data);
            });
        });
        // 3. 数据转换
        totalTasks.push(function (data, callback) {
            docConvertUtil.covertApiDocStringToOpenApiDoc(data.apiResult.data.docString, function (error, openDoc) {
                if (error){
                   return callback(CONSTANT.CODE.UNKNOWN_ERR,null);
                }
                data.retData = openDoc;
                return callback(null, data);
            });
        });
        async.waterfall(totalTasks, function (error, data) {
            if(error) return callback(error, null);
            return callback(error, {design_id : designId,content:data.retData});
        });
    };

    return designsVideosUtil;
})();

module.exports = designsVideosUtil;